package com.zhipu.balance.utils.print.rapeseed

import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.mobile.event.RecordRapeseed
import com.zhipu.balance.mobile.event.RecordSoybean
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.print.BrightekCommandM
import java.nio.charset.Charset

/**
 * 油菜籽打印
 */
object RapeseedPrintUtils {

    /**
     * 油菜籽打印
     */
    fun SoybeanPrinting(mRecordRapeseed : RecordRapeseed){
        val stringBuilder = StringBuilder()
        val title = "油菜籽打印"
        val retract = ((1.0 - title.length / 16.0) * 16).toInt()
        for (i in 0 until retract) {
            stringBuilder.append(" ")
        }
        stringBuilder.append(title).append('\r')
        //编号
        //编号
        stringBuilder.append("编号：").append(mRecordRapeseed.uniqueId).append('\r')
        //时间
        //时间
        stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordRapeseed.inspectionDate)).append('\r')
        /**
         * 杂质记录员
         */
        if(!TextUtils.isEmpty(mRecordRapeseed.impurityRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("杂质").append('\r')
            stringBuilder.append("\r")
            // 大样试验-试样质量
            stringBuilder.append("大样质量(G)：").append(mRecordRapeseed.largeMiscellaneousSampleQuality).append('\r');
            //大样试验-杂质
            stringBuilder.append("大样杂质(G)：").append(mRecordRapeseed.detailImpurity).append('\r');
            //大样杂质率
            stringBuilder.append("大样杂质率(%)：").append(mRecordRapeseed.detailResults).append('\r');
            //小样试验-试样质量
            stringBuilder.append("小样质量(G)：").append(mRecordRapeseed.smallMiscellaneousSampleQuality).append('\r');
            //小样试验-杂质
            stringBuilder.append("小样杂质(G)：").append(mRecordRapeseed.smallSampleImpurity).append('\r');
            //小样杂质率
            stringBuilder.append("小样杂质率(%)：").append(mRecordRapeseed.sampleResults).append('\r');
            //杂质总量
            stringBuilder.append("杂质总量(%)：").append(mRecordRapeseed.impurityResults).append('\r');
            //杂质记录员
            stringBuilder.append("杂质记录员：").append(mRecordRapeseed.impurityRecorder).append('\r');
        }
        //生芽粒率记录员
        if(!TextUtils.isEmpty(mRecordRapeseed.sproutRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("生芽粒").append('\r');
            stringBuilder.append("\r");
            //生芽粒质量
            stringBuilder.append("生芽粒质量(G)：").append(mRecordRapeseed.sproutWeight).append('\r');
            //生芽粒率
            stringBuilder.append("生芽粒率(%)：").append(mRecordRapeseed.sproutResult).append('\r');
            //生芽粒率记录员
            stringBuilder.append("生芽粒率记录员：").append(mRecordRapeseed.sproutRecorder).append('\r');
        }

        //生霉粒率记录员
        if(!TextUtils.isEmpty(mRecordRapeseed.moldRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("生霉粒").append('\r');
            stringBuilder.append("\r");

            //生霉粒质量
            stringBuilder.append("生霉粒质量(G)：").append(mRecordRapeseed.moldWeight).append('\r');
            //生霉粒率
            stringBuilder.append("生霉粒率(%)：").append(mRecordRapeseed.moldResult).append('\r');
            //生霉粒率记录员
            stringBuilder.append("生霉粒率记录员：").append(mRecordRapeseed.moldRecorder).append('\r');
        }

        //热损伤粒数记录员
        if(!TextUtils.isEmpty(mRecordRapeseed.thermalDamageNumberRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("热损伤粒").append('\r');
            stringBuilder.append("\r");
            //第一次
            stringBuilder.append("第一次：").append(mRecordRapeseed.thermalDamageNumber1).append('\r');
            //第二次
            stringBuilder.append("第二次：").append(mRecordRapeseed.thermalDamageNumber2).append('\r');
            //热损伤粒
            stringBuilder.append("热损伤粒(%)：").append(mRecordRapeseed.thermalDamageGrain).append('\r');
            //热损伤粒记录员
            stringBuilder.append("热损伤粒记录员：").append(mRecordRapeseed.thermalDamageNumberRecorder).append('\r');
        }

        //未熟粒记录员
        if(!TextUtils.isEmpty(mRecordRapeseed.immatureGrainNumberRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("未熟粒").append('\r');
            stringBuilder.append("\r");
            //第一次
            stringBuilder.append("第一次：").append(mRecordRapeseed.immatureGrainNumber1).append('\r');
            //第二次
            stringBuilder.append("第二次：").append(mRecordRapeseed.immatureGrainNumber2).append('\r');
            //未熟粒
            stringBuilder.append("未熟粒(%)：").append(mRecordRapeseed.immatureGrainResult).append('\r');
            //未熟粒记录员
            stringBuilder.append("未熟粒记录员：").append(mRecordRapeseed.immatureGrainNumberRecorder).append('\r');
        }


        var i = 0
        while (i < (1.0 - 2 / 16.0) * 16) {
            stringBuilder.append(" ")
            i++
        }
        InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
        InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
    }


    /**
     * 油菜籽打印
     */
    fun SoybeanPrinting(mRecordRapeseedList: List<RecordRapeseed>,mSubclass: String){
        for (mRecordRapeseed in mRecordRapeseedList){
            if(!mRecordRapeseed.select){
                continue
            }
            val stringBuilder = StringBuilder()
            val title = "油菜籽打印"
            val retract = ((1.0 - title.length / 16.0) * 16).toInt()
            for (i in 0 until retract) {
                stringBuilder.append(" ")
            }
            stringBuilder.append(title).append('\r')
            //编号
            stringBuilder.append("编号：").append(mRecordRapeseed.uniqueId).append('\r')
            //时间
            stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordRapeseed.inspectionDate)).append('\r')
            when (mSubclass) {
                "杂质" -> {
                    /**
                     * 杂质记录员
                     */
                    if(!TextUtils.isEmpty(mRecordRapeseed.impurityRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("杂质").append('\r')
                        stringBuilder.append("\r")
                        // 大样试验-试样质量
                        stringBuilder.append("大样质量(G)：").append(mRecordRapeseed.largeMiscellaneousSampleQuality).append('\r');
                        //大样试验-杂质
                        stringBuilder.append("大样杂质(G)：").append(mRecordRapeseed.detailImpurity).append('\r');
                        //大样杂质率
                        stringBuilder.append("大样杂质率(%)：").append(mRecordRapeseed.detailResults).append('\r');
                        //小样试验-试样质量
                        stringBuilder.append("小样质量(G)：").append(mRecordRapeseed.smallMiscellaneousSampleQuality).append('\r');
                        //小样试验-杂质
                        stringBuilder.append("小样杂质(G)：").append(mRecordRapeseed.smallSampleImpurity).append('\r');
                        //小样杂质率
                        stringBuilder.append("小样杂质率(%)：").append(mRecordRapeseed.sampleResults).append('\r');
                        //杂质总量
                        stringBuilder.append("杂质总量(%)：").append(mRecordRapeseed.impurityResults).append('\r');
                        //杂质记录员
                        stringBuilder.append("杂质记录员：").append(mRecordRapeseed.impurityRecorder).append('\r');
                    }
                }
                "生芽粒" -> {
                    //生芽粒率记录员
                    if(!TextUtils.isEmpty(mRecordRapeseed.sproutRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("生芽粒").append('\r');
                        stringBuilder.append("\r");
                        //生芽粒质量
                        stringBuilder.append("生芽粒质量(G)：").append(mRecordRapeseed.sproutWeight).append('\r');
                        //生芽粒率
                        stringBuilder.append("生芽粒率(%)：").append(mRecordRapeseed.sproutResult).append('\r');
                        //生芽粒率记录员
                        stringBuilder.append("生芽粒率记录员：").append(mRecordRapeseed.sproutRecorder).append('\r');
                    }
                }
                "生霉粒" -> {
                    //生霉粒率记录员
                    if(!TextUtils.isEmpty(mRecordRapeseed.moldRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("生霉粒").append('\r');
                        stringBuilder.append("\r");

                        //生霉粒质量
                        stringBuilder.append("生霉粒质量(G)：").append(mRecordRapeseed.moldWeight).append('\r');
                        //生霉粒率
                        stringBuilder.append("生霉粒率(%)：").append(mRecordRapeseed.moldResult).append('\r');
                        //生霉粒率记录员
                        stringBuilder.append("生霉粒率记录员：").append(mRecordRapeseed.moldRecorder).append('\r');
                    }
                }
                "热损伤粒" -> {
                    //热损伤粒数记录员
                    if(!TextUtils.isEmpty(mRecordRapeseed.thermalDamageNumberRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("热损伤粒").append('\r');
                        stringBuilder.append("\r");
                        //第一次
                        stringBuilder.append("第一次：").append(mRecordRapeseed.thermalDamageNumber1).append('\r');
                        //第二次
                        stringBuilder.append("第二次：").append(mRecordRapeseed.thermalDamageNumber2).append('\r');
                        //热损伤粒
                        stringBuilder.append("热损伤粒(%)：").append(mRecordRapeseed.thermalDamageGrain).append('\r');
                        //热损伤粒记录员
                        stringBuilder.append("热损伤粒记录员：").append(mRecordRapeseed.thermalDamageNumberRecorder).append('\r');
                    }
                }
                "未熟粒" -> {
                    //未熟粒记录员
                    if(!TextUtils.isEmpty(mRecordRapeseed.immatureGrainNumberRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("未熟粒").append('\r');
                        stringBuilder.append("\r");
                        //第一次
                        stringBuilder.append("第一次：").append(mRecordRapeseed.immatureGrainNumber1).append('\r');
                        //第二次
                        stringBuilder.append("第二次：").append(mRecordRapeseed.immatureGrainNumber2).append('\r');
                        //未熟粒
                        stringBuilder.append("未熟粒(%)：").append(mRecordRapeseed.immatureGrainResult).append('\r');
                        //未熟粒记录员
                        stringBuilder.append("未熟粒记录员：").append(mRecordRapeseed.immatureGrainNumberRecorder).append('\r');
                    }
                }
            }
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
            InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
        }
    }
}